package MyTest;

//创建一个自定义类
class myThread {
    int count = 0;
    public void run() {
        synchronized (this){
            count++;
        }
    }
    public int getCount() {
        return count;
    }
}
public class TestDemo5 {
    public static void main(String[] args) throws InterruptedException {
        myThread myThread = new myThread();//实例化这个类
        Thread thread1 = new Thread(()-> {
            for (int i = 0; i < 10000; i++) {
                myThread.run();
            }
        });
        Thread thread2 = new Thread(()-> {
            for (int i = 0; i < 10000; i++) {
                myThread.run();
            }
        });
        thread1.start();//启动线程thread1
        thread2.start();//启动线程thread2
        thread1.join();//等待线程thread1结束
        thread2.join();//等待线程thread2结束
        System.out.println(myThread.getCount());//获取count值
    }
}